use anyhow::Result;
use axum::Extension;
use std::net::SocketAddr;

use crate::config::load_config;
use crate::core::engine::OpsPanelCore;

mod api;
mod config;
mod core;
mod external;

#[tokio::main]
async fn main() -> Result<()> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .init();

    // 加载配置
    let config = config::load_config(None)?;

    // 创建核心引擎
    let ops_core = OpsPanelCore::new(config).await?;

    // 创建路由
    let app = api::create_router().layer(Extension(ops_core));

    // 启动服务器
    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
    tracing::info!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await?;

    Ok(())
}